Imports System.Collections
Imports System.IO
Imports SchemaCodeGen

Partial Class pages_Templates_default : Inherits CPage

#Region "Querystring (Filters)"
	Public ReadOnly Property Search As String
		Get
			Return CWeb.RequestStr("search")
		End Get
	End Property

	'Rename or Delete:
	'Public ReadOnly Property ParentId As Integer
	'    Get
	'        Return CWeb.RequestStr("parentId")
	'    End Get
	'End Property
#End Region

#Region "Data"
	Public ReadOnly Property [Templates]() As CTemplateList
		Get
			Return CTemplate.Cache.Search(txtSearch.Text) 'Paging done in usercontrol
		End Get
	End Property
#End Region

#Region "Event Handlers - Page "
	Protected Overrides Sub PageInit()
		'Populate Dropdowns
		Dim ignore As String() = New String() {}
		Dim path As String = "C:\TFS06\jeremydotnet\Common\CodeGenerator\templates\dotnet\vbnet\"
		Dim fh As New CFolderHash(path, ignore)
		Dim ff As New CFilesList(path, ignore)
		For Each i As CFileHash In fh
			Dim t As CTemplate = CTemplate.Cache.GetByName(i.Name)
			If IsNothing(t) Then
				t = New CTemplate
				t.TemplateName = i.Name
				t.Save()
			End If
			If 1 >= t.Contents.Count Then
				Dim c As New CContent()
				c.ContentSetId = ETemplateSet.VB_NET_Default
				c.ContentTemplateId = t.TemplateId
				c.ContentText = File.ReadAllText(path & i.Name)
				c.Save()
			End If
		Next


		'Search state (from querystring)
		txtSearch.Text = Me.Search

		'Display Results
		ctrlTemplates.Display(Me.Templates)

		'Client-side
		Me.Form.DefaultFocus = txtSearch.ClientID
		Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)
	End Sub
#End Region

#Region "Event Handlers - Form"
	Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
		Response.Redirect(CSitemap.Templates(txtSearch.Text))
	End Sub
#End Region

#Region "Event Handlers - UserControl"
	Private Sub ctrl_ExportClick() Handles ctrlTemplates.ExportClick
		Templates.ExportToCsv(Response, "Templates.csv")
	End Sub
	Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlTemplates.ResortClick
		Response.Redirect(CSitemap.Templates(txtSearch.Text, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
	End Sub
#End Region

End Class
